package com.xzy.demo.controller;

import com.xzy.mybatis.pojo.User;
import com.xzy.mybatis.service.UserMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
public class LoginController {

    public static Logger logger = LoggerFactory.getLogger(LoginController.class);
    @Autowired private UserMapper userMapper;

    @RequestMapping("/login")
    public String index(Model model, HttpServletRequest request, User user){
        int localPort = request.getLocalPort();
        if(user!=null&& StringUtils.hasText(user.getName())){
            User obj = userMapper.getUserByName(user.getName());
            if(obj.getPassword().equals(user.getPassword())){
                model.addAttribute("status",true);
                request.getSession().setAttribute("msg", "登录成功");
                request.getSession().setAttribute("userName", obj.getName());
                return "dashboard";
            }else{
                model.addAttribute("status",false);
                model.addAttribute("msg","密码错误");
                return "index";
            }
        }
        logger.info("端口号【"+localPort+"】：进入首页");
        return "index";
    }



}
